package share.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import share.service.GrossRevenueReportService;
import share.service.model.GrossRevenueReportModel;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("/grossRevenueReport")
public class  GrossRevenueReportController {

    private static Logger log = LoggerFactory.getLogger(GrossRevenueReportController.class);

    @Autowired
    private GrossRevenueReportService grossRevenueReportService;

    @GetMapping("save")
    public String addGrossRevenueReport(@RequestParam(name = "id")Integer id, Model model) {
        model.addAttribute("shareId", id);
        return "grossRevenueReport/addGrossRevenueReport";
    }

    @PostMapping("save")
    @ResponseBody
    public String addGrossRevenueReport(@ModelAttribute GrossRevenueReportModel grrModel) {
        grossRevenueReportService.addGrossRevenueReport(grrModel);
        return grrModel.toString();
    }

    @InitBinder("grrModel")
    public void initDate1(WebDataBinder binder){
        binder.registerCustomEditor(Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),true));
    }

}
